#include "chat_interface.h"

chat_interface::chat_interface(const QString &otherUserName,QWidget *parent)
    : QWidget(parent)
{
    //设置窗口布局
    QVBoxLayout *mainLayout=new QVBoxLayout(this);
    mainLayout->setContentsMargins(0,0,0,0);
    mainLayout->setSpacing(0);

    //添加对方聊天名称
    QHBoxLayout *topLayout=new QHBoxLayout(this);

    QLabel *nameLabel=new QLabel(otherUserName,this);
    QPushButton *information=new QPushButton(this);
    information->setObjectName("information");
    information->setIcon(QIcon(":/new/prefix1/img/information.png"));
    information->setCursor(Qt::PointingHandCursor);
    nameLabel->setStyleSheet("font-size:32px;font-weight:bold;padding:10px;background-color:#f0f0f0;font-family:heiti");
    topLayout->addWidget(nameLabel);
    topLayout->addStretch();
    topLayout->addWidget(information);
    topLayout->setContentsMargins(10,10,10,10);
    mainLayout->addLayout(topLayout);

    //添加聊天区域
    QScrollArea *scrollArea=new QScrollArea(this);
    scrollArea->setWidgetResizable(true);  // 确保内容部件可以调整大小
    scrollArea->setHorizontalScrollBarPolicy(Qt::ScrollBarAlwaysOff);
    scrollArea->setMinimumSize(600,400);
    scrollArea->setObjectName("scrollArea");
    scrollArea->setStyleSheet("QScrollArea {background-color:#ececec;border:0px;}");


    //创建内容部件
    QWidget *contentWidget=new QWidget(this);
    contentLayout=new QVBoxLayout(contentWidget);
    contentLayout->setContentsMargins(10,10,10,10);
    contentLayout->setSpacing(10);

    contentLayout->addStretch();

    //设置内容部件
    scrollArea->setWidget(contentWidget);
    mainLayout->addWidget(scrollArea);

    //添加功能按钮区域
    QWidget *buttonWidget=new QWidget(this);
    QHBoxLayout *pb_group=new QHBoxLayout(buttonWidget);
    pb_group->setContentsMargins(10,10,10,10);
    QPushButton *face=new QPushButton(buttonWidget);
    face->setIcon(QIcon(":/new/prefix1/img/face.png"));
    face->setCursor(Qt::PointingHandCursor);
    QPushButton *file=new QPushButton();
    file->setIcon(QIcon(":/new/prefix1/img/chat_file.png"));
    file->setCursor(Qt::PointingHandCursor);
    QPushButton *cut=new QPushButton(buttonWidget);
    cut->setIcon(QIcon(":/new/prefix1/img/cut.png"));
    cut->setCursor(Qt::PointingHandCursor);
    QPushButton *chat_history=new QPushButton(buttonWidget);
    chat_history->setIcon(QIcon(":/new/prefix1/img/chat-history.png"));
    chat_history->setCursor(Qt::PointingHandCursor);
    QPushButton *voice=new QPushButton(buttonWidget);
    voice->setIcon(QIcon(":/new/prefix1/img/voice.png"));
    voice->setCursor(Qt::PointingHandCursor);
    QPushButton *video=new QPushButton(buttonWidget);
    video->setIcon(QIcon(":/new/prefix1/img/video.png"));
    video->setCursor(Qt::PointingHandCursor);

    pb_group->addWidget(face);
    pb_group->addWidget(file);
    pb_group->addWidget(cut);
    pb_group->addWidget(chat_history);
    pb_group->addStretch();
    pb_group->addWidget(voice);
    pb_group->addWidget(video);

    mainLayout->addWidget(buttonWidget);

    // 设置 buttonWidget 的对象名
    buttonWidget->setObjectName("buttonWidget");

    //添加消息输入框和发送按钮
    QWidget *inputWidget=new QWidget(this);
    QVBoxLayout *inputLayout=new QVBoxLayout(inputWidget);
    inputLayout->setContentsMargins(10,10,10,10);

    //消息输入框
    QTextEdit *messageInput=new QTextEdit(inputWidget);
    messageInput->setObjectName("messageInput");  // 设置对象名
    messageInput->setPlaceholderText("请输入消息...");
    messageInput->setMaximumHeight(300);
    inputLayout->addWidget(messageInput, 1);  // 输入框占据大部分空间

    // 发送按钮
    QPushButton *sendButton = new QPushButton("发送(S)", inputWidget);
    sendButton->setFixedSize(156, 48);  // 固定按钮大小
    sendButton->setObjectName("sendButton");
    inputLayout->addWidget(sendButton, 0, Qt::AlignBottom | Qt::AlignRight);  // 按钮固定在右下角

    mainLayout->addWidget(inputWidget);

    setLayout(mainLayout);

}

//添加自身消息
void chat_interface::addSelfMessage(const QString &avatarPath, const QString &message)
{
    QWidget *widget = new QWidget(this);
    QHBoxLayout *layout = new QHBoxLayout(widget);
    layout->setContentsMargins(10, 5, 10, 5);

    // 头像
    QLabel *avatarLabel = new QLabel(widget);
    avatarLabel->setPixmap(QPixmap(avatarPath).scaled(48, 48, Qt::KeepAspectRatio, Qt::SmoothTransformation));
    avatarLabel->setStyleSheet("border-radius: 20px;");
    avatarLabel->setFixedSize(48, 48);  // 固定头像大小

    // 消息内容
    QLabel *messageLabel = new QLabel(message, widget);
    messageLabel->setStyleSheet("background-color: #e1f3d8; border-radius: 10px; padding: 10px;");
    messageLabel->setWordWrap(true);
    messageLabel->setMaximumWidth(480);  // 限制消息宽度
    messageLabel->setMinimumHeight(48);
    messageLabel->setSizePolicy(QSizePolicy::Preferred, QSizePolicy::Preferred);
    messageLabel->setMinimumSize(messageLabel->sizeHint());  // 根据内容设置最小大小

    // 布局：头像在右，消息在左
    layout->addWidget(messageLabel, 1, Qt::AlignRight | Qt::AlignTop);  // 消息顶部对齐
    layout->addWidget(avatarLabel, 0, Qt::AlignRight | Qt::AlignTop);   // 头像顶部对齐

    widget->setLayout(layout);

    contentLayout->insertWidget(contentLayout->count() - 1, widget);  // 插入到弹簧之前
}

//添加对方消息
void chat_interface::addOtherMessage(const QString &avatarPath, const QString &message)
{
    QWidget *widget = new QWidget(this);
    QHBoxLayout *layout = new QHBoxLayout(widget);
    layout->setContentsMargins(10, 5, 10, 5);

    // 头像
    QLabel *avatarLabel = new QLabel(widget);
    avatarLabel->setPixmap(QPixmap(avatarPath).scaled(48, 48, Qt::KeepAspectRatio, Qt::SmoothTransformation));
    avatarLabel->setStyleSheet("border-radius: 20px;");
    avatarLabel->setFixedSize(48, 48);  // 固定头像大小

    // 消息内容
    QLabel *messageLabel = new QLabel(message, widget);
    messageLabel->setStyleSheet("background-color: #ffffff; border-radius: 10px; padding: 10px;");
    messageLabel->setWordWrap(true);
    messageLabel->setMaximumWidth(480);  // 限制消息宽度
    messageLabel->setMinimumHeight(48);
    messageLabel->setSizePolicy(QSizePolicy::Preferred, QSizePolicy::Preferred);
    messageLabel->setMinimumSize(messageLabel->sizeHint());  // 根据内容设置最小大小


    // 布局：头像在左，消息在右
    layout->addWidget(avatarLabel, 0, Qt::AlignLeft | Qt::AlignTop);   // 头像顶部对齐
    layout->addWidget(messageLabel, 1, Qt::AlignLeft | Qt::AlignTop);  // 消息顶部对齐

    widget->setLayout(layout);

    contentLayout->insertWidget(contentLayout->count() - 1, widget);  // 插入到弹簧之前
}

void chat_interface::addTimeLabel(const QString &time)
{
    QWidget *widget = new QWidget(this);
    QHBoxLayout *layout = new QHBoxLayout(widget);
    layout->setContentsMargins(10, 5, 10, 5);

    QLabel *timeLabel = new QLabel(time, widget);
    timeLabel->setStyleSheet("color: #999999; font-size: 12px;");
    timeLabel->setAlignment(Qt::AlignCenter);

    layout->addWidget(timeLabel);
    widget->setLayout(layout);

    contentLayout->insertWidget(contentLayout->count()-1, widget);
}

chat_interface::~chat_interface()
{

}

